package me.like.diandian.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;

import java.util.List;

import me.like.diandian.R;
import me.like.diandian.activity.ExploreActivity;
import me.like.diandian.api.Api;
import me.like.diandian.base.BaseActivity;
import me.like.diandian.base.BaseConstant;
import me.like.diandian.bean.LoanMarketListBean;
import me.like.diandian.databinding.ItemLoanBinding;
import me.like.diandian.requestbean.HoweLoanMarketCountDto;
import me.like.diandian.util.RxUtils;

import static com.jakewharton.rxbinding2.view.RxView.clicks;

/**
 * Created by shousakai on 2019/6/21.
 */

public class DaiKuanListAdapter extends RecyclerView.Adapter<DaiKuanListAdapter.DaiKuanListViewHolder> {
    public void setDatas(List<LoanMarketListBean> datas) {
        this.datas.clear();
        this.datas.addAll(datas);
        notifyDataSetChanged();
    }

    private List<LoanMarketListBean> datas;
    private BaseActivity context;

    public DaiKuanListAdapter(BaseActivity activity, List<LoanMarketListBean> datas) {
        context = activity;
        this.datas = datas;
    }


    @NonNull
    @Override
    public DaiKuanListViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_loan, null);
        return new DaiKuanListViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull DaiKuanListViewHolder daiKuanListViewHolder, int i) {
        LoanMarketListBean item = datas.get(i);
        Glide.with(context).load(item.getLogoUrl()).into(daiKuanListViewHolder.binding.ivImage);
        daiKuanListViewHolder.binding.setData(item);
        clicks(daiKuanListViewHolder.binding.getRoot()).subscribe(v -> {
            Api.getInstance().getService().countLoanMarketAppRegist(new HoweLoanMarketCountDto
                    (BaseConstant.APPCHANNELID, item.getLoanId()))
                    .compose(RxUtils.applySchedulerAndAllFilter(context)).subscribe(data -> {

            }, Throwable::printStackTrace);
            ExploreActivity.startActivity(context, item.getH5JumpUrl(), item.getProjectName());
        });
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    class DaiKuanListViewHolder extends RecyclerView.ViewHolder {
        ItemLoanBinding binding;

        public DaiKuanListViewHolder(@NonNull View itemView) {
            super(itemView);
            binding = DataBindingUtil.bind(itemView);
        }
    }
}
